package com.example.filetransfer.util;

import java.io.*;
import java.util.Properties;

public class VersionChecker {
    private static final String VERSION_FILE = "version.properties";
    private static final String CURRENT_VERSION = "1.0.0";
    private static final String LATEST_VERSION = "2.0.0";

    // 检查本地是否已是最新版本
    public static boolean checkForUpdates() {
        Properties props = new Properties();
        File file = new File(VERSION_FILE);
        if (file.exists()) {
            try (FileInputStream fis = new FileInputStream(file)) {
                props.load(fis);
                String updated = props.getProperty("updated", "false");
                String version = props.getProperty("version", CURRENT_VERSION);
                // 如果已更新且版本号为最新，则不再提示
                if ("true".equals(updated) && LATEST_VERSION.equals(version)) {
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 模拟服务器有新版本
        return true;
    }

    // 伪更新：弹窗后写本地version.properties，标记为最新
    public static boolean downloadNewVersion() {
        Properties props = new Properties();
        props.setProperty("updated", "true");
        props.setProperty("version", LATEST_VERSION);
        try (FileOutputStream fos = new FileOutputStream(VERSION_FILE)) {
            props.store(fos, "Client version info");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
} 